package com.rpc.server;

import com.rpc.threadManager.WorkerThread;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.*;

public class RpcServer {
    private final ExecutorService threadPool;
    public RpcServer(){
        int corePoolSize=5;
        int maxmumPoolSize=15;
        long keepAliveTime=60;
        BlockingQueue<Runnable> queue=new ArrayBlockingQueue<>(100);
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        threadPool= new ThreadPoolExecutor(corePoolSize, maxmumPoolSize, keepAliveTime, TimeUnit.SECONDS, queue, threadFactory);
    }

    public void register(Object obj,int port){
        try( ServerSocket serverSocket = new ServerSocket(port)) {
            Socket socket;
            while ((socket=serverSocket.accept())!=null){
                System.out.println("客户端有连接，ip为："+socket.getInetAddress());
                threadPool.execute(new WorkerThread(socket,obj));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
